
$(document).ready(function(){
	loadRoles();
});

var roleOrgTree,roleModuleTree;	
	/**
	 * 点击选择按钮加载组织树
	 */
	var roleOrgSetting = {
		view: {
			dblClickExpand: false
		},
		data:{
			key:{
				name:"name"
			},
			simpleData:{
				enable:true,
				idKey:"orgId",
				pIdKey:"parentOrgId",
				rootPId:0
			}
		},
		callback: {
			onClick: roleOnClick
		}
	};
	
	function roleOnClick(e, treeId, treeNode) {
		var orgName = treeNode.name;
		var orgId = treeNode.orgId;
		var orgObj = $("#rname");
		orgObj.attr("value", orgName);
		$("#rorgId").val(orgId);
		roleHideMenu();
	}
	
	function showRoleOrgTree() {
		var url = appCtx + "secure/role/loadRoleOrgData.html";
		$.post(url,function(data){ 
			roleOrgTree = $.fn.zTree.init($("#roleOrgTree"), roleOrgSetting, data);
		});
		
		var orgObj = $("#rname");
		var roleOrgOffset = $("#rname").offset();
		$("#rmenuContent").css({left:roleOrgOffset.left + "px", top:(roleOrgOffset.top + orgObj.outerHeight()-76) + "px"}).show();
		$("body").bind("mousedown", roleOnBodyDown);
		
	}
	
	function roleHideMenu() {
		$("#rmenuContent").fadeOut("fast");
		$(".roleView").unbind("mousedown", roleOnBodyDown);	
	}
	
	function roleOnBodyDown(event) {
		if (!(event.target.id == "menuBtn" || event.target.id == "rmenuContent" || $(event.target).parents("#rmenuContent").length>0)) {
			roleHideMenu();
		}
	}
	/*
	 * 角色模块权限树
	 * **/
	var roleModuleSetting = {
			view: {
				dblClickExpand: false
			},
			check: {
				enable: true
			},
			data:{
				key:{
					name:"moduleName"
				},
				simpleData:{
					enable:true,
					idKey:"moduleId",
					pIdKey:"parentModuleId",
					rootPId:0
				}
			}
		};
		
		

	/**
	 * 角色信息列表
	 */
	function loadRoles(){
		$("#RoleContainer").jtable({
			title:"",
			paging:true,
			pageSize:10,
			gotoPageArea:'none',
			columnSelectable: false,
			pageSizeChangeArea:false,
			selectingCheckboxes:true,
			selecting: true, //Enable selecting
            multiselect: true, //Allow multiple selecting
            //selectOnRowClick: false, //Enable this to only select using checkboxes
			actions:{
				listAction:appCtx + "secure/role/loadAllRoles.html"
			},
			fields:{
				roleId:{
					key:true,
					list:false
				},
				
				roleName:{
					title:"角色名称",
					width:"10%"
				},
				roleCode:{
					title:"角色编码",
					width:"10%"
				},
				orgName:{
					title:"所属组织",
					width:"10%"
				},
				remark:{
					title:"备注",
					width:"10%"
				},
				operation:{
					title:"操作",
					width:"5%",
					userData:true,
					content:$("#operationRole").html(),
					param:"roleId"
				}
			}
		});
		
		queryRole();
	}

	function queryRole(){
		var params = $("#queryRoleForm").serialize();
		$("#RoleContainer").jtable("load",params);
	}
	function roleResets(){	
		$("#rname").attr("value","");
		$("#rorgId").attr("value","");
		return;
	}
	//查看角色
	function lookRole(roleId,roleName){
		var url = appCtx + "secure/role/listRoleUser.html";
		$.post(url,function(data){
			tabpanel.addTab({id:"roleUser",title:"查看角色",html:data});
		}).done(function(){
			//加载模块权限树
			var urlRoleModule = appCtx + "secure/module/loadModulesData.html";
			$.post(urlRoleModule,function(data){ 
				roleModuleTree=$.fn.zTree.init($("#roleModuleTree"), roleModuleSetting, data);
				parseRoleModuleTreeIds(roleModuleTree);
			});
			loadRoleUser(roleId,roleName);
		});
	}
	/**
	 * 
	 * @param roleModuleTree
	 * @returns
	function parseRoleModuleTreeIds(roleModuleTree);{
		var moduleIds = $("#moduleIds").val();
		if(moduleIds!=""){
			roleModuleTree.expandAll(true);
			var array = moduleIds.split(",");
			for(var i=0;i<array.length;i++){
				var currentNode = roleModuleTree.getNodeByParam("moduleId",array[i],null);
				roleModuleTree.checkNode(currentNode,true,true);
			}
		}
	}*/
	
	function loadRoleUser(roleId,title){
		alert(roleId);
		$("#roleUserContainer").jtable({
			title:title+" 用户列表",
			paging:true,
			pageSize:10,
			gotoPageArea:'none',
			columnSelectable: false,
			pageSizeChangeArea:false,
			selectingCheckboxes:true,
			selecting: true, //Enable selecting
            multiselect: true, //Allow multiple selecting
            //selectOnRowClick: false, //Enable this to only select using checkboxes
			actions:{
				listAction:appCtx + "secure/role/loadRoleUsers.html?roleId="+roleId
			},
			fields:{
				userId:{
					key:true,
					list:false
				},
				userName:{
					title:"用户名称",
					width:"10%"
				},
				loginName:{
					title:"登陆账号",
					width:"10%"
				},
				orgId:{
					title:"所属部门",
					width:"10%"
				}
			}
		});		
		$("#roleUserContainer").jtable("load");
	}